# coding=utf-8
"""
Author: Mr.夏末
@Function: 接口传参
"""
import json

import requests

#带在URL后面
res1 = requests.get(
    'https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=111.1.1.1&co=&resource_id=5809&t=1649684836325&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110207084776351552851_1649684823820&_=1649684823826')
# print(res1.content.decode('unicode-escape'))
res1 = res1.text
res1 = res1[res1.find('{'):res1.rfind('}')+1]
print(json.loads(res1))

#使用params=字典，传url参数
p = {
    'query': '122.1.1.1', 'co': '', 'resource_id': '5809', 't': '1649684836325', 'ie': 'utf8', 'oe': 'gbk',
    'cb': 'op_aladdin_callback', 'format': 'json', 'tn': 'baidu', 'cb': 'jQuery110207084776351552851_1649684823820',
    '_': '1649684823826'
}
res2 = requests.get('https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php',params=p)
# print(res2.content.decode('unicode-escape'))


#Content-Type: application/x-www-form-urlencoded
params = {'username': 'Will', 'password': '1234561'}
# 调用接口
res3 = requests.post('http://testingedu.com.cn:8081/inter/HTTP/login',data=params)
# print(res3.text)

#json传参: 标准
#Content-Type: application/json
res4 = requests.post('http://39.108.55.18/mypro/api/user/login',
             json={"username":"Will","pwd":"123456"})
print(res4.content.decode('unicode-escape'))

#JSON字符串不标准传参，建议上一种
res4 = requests.post('http://39.108.55.18/mypro/api/user/login',
             data='{"username":"Will","pwd":"123456"}')
print(res4.content.decode('unicode-escape'))